package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.*;
import com.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.Inet4Address;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService service;
    @Autowired
    private IBuyCarService buyCarService;
    @Autowired
    private IBuyOrderService buyOrderService;
    @Autowired
    private IBuyOrderInfoService buyOrderInfoService;
    @Autowired
    private ISaleOrderService saleOrderService;
    @Autowired
    private ISaleCarService saleCarService;
    @Autowired
    private ISaleOrderInfoService saleOrderInfoService;



    @RequestMapping("findByItem")
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum, String salePrice, String type) {
        return service.findByItem(pageNO, name, startNum, endNum, salePrice, type);
    }

    @RequestMapping("findById")
    public ProductBean findById(Integer id) {
        return service.findById(id);
    }

    @RequestMapping("updateSalePrice")
    public String updateSalePrice(Integer id, Integer newSalePrice) {
        service.updateSalePrice(id,newSalePrice);
        return "ok";
    }


    @RequestMapping("inKu")
    public String inKu(Integer userId,Integer vendorId) {
        //添加采购订单
        BuyOrderBean buyOrder=new BuyOrderBean();
        buyOrder.setVendorId(vendorId);
        buyOrder.setUserId(userId);
        buyOrderService.add(buyOrder);
        Integer buyOrderId= buyOrder.getId();
        //按采购员ID查询采购车集合
        List<BuyCarBean> buyCarList=buyCarService.findByUserId(userId);
        //遍历采购车，循环添加采购明细信息
        for (BuyCarBean buyCar:buyCarList){
            BuyOrderInfoBean buyOrderInfo=new BuyOrderInfoBean(buyOrderId,buyCar.getProductId(),buyCar.getPrice(),buyCar.getNum());
            buyOrderInfoService.add(buyOrderInfo);
            //修改库存商品的库存数量（累加）、最新采购价
            service.updateNum(buyCar.getProductId(),buyCar.getPrice(),buyCar.getNum(), "isInKu");
        }
        //根据采购员id清空采购车
        buyCarService.delByUserId(userId);
        return "ok";
    }

    @RequestMapping("outKu")
    public String outKu(SaleOrderBean saleOrder) {
//      添加销售基本信息
        saleOrderService.add(saleOrder);
        Integer saleOrderId=saleOrder.getId();

//      根据销售员id查询销售车信息集合
        List<SaleCarBean> saleCarList=saleCarService.findByUserId(saleOrder.getUserId());

//      遍历销售车集合，循环添加销售明细，循环修改库存数量（累减）
        for (SaleCarBean saleCar:saleCarList){
            //添加销售明细
            SaleOrderInfoBean saleOrderInfo=new SaleOrderInfoBean(saleOrderId,saleCar.getProductId(),saleCar.getProduct().getSalePrice(),saleCar.getNum());
            saleOrderInfoService.add(saleOrderInfo);
            //修改库存数量（累减）
            service.updateNum(saleCar.getProductId(),saleCar.getProduct().getSalePrice(),saleCar.getNum(), "");
        }

//      根据销售员id清空销售车
        saleCarService.delByUser(saleOrder.getUserId());
        return "ok";
    }

}
